#include "EntityEnemy.h"
#include "ComponentManager.h"

CEntityEnemy::CEntityEnemy(const CVector2i& initialPosition, int range, int animWalkLeft, 
						   int animWalkRight, int hp, int damagePoint, int speed) :
						   CWorldEntity(0, initialPosition, true), m_rangeOfDisplacement(range), 
						   m_animWalkLeft(animWalkLeft), m_animWalkRight(animWalkRight), 
						   m_initialPosition(initialPosition), m_speed(speed), m_animTime(-1), m_idAnimSprite(-1)
{
	m_healthPoint = hp;
	m_damagePoint = damagePoint;
	m_goToTheRight = true;
	m_side = eSideEnemy;

	m_rectangleCollision.setTop(0);
	m_rectangleCollision.setBottom(CM.VISU().getAnimationSpriteHeight(m_animWalkLeft));
	m_rectangleCollision.setLeft(0);
	m_rectangleCollision.setRight(CM.VISU().getAnimationSpriteWidth(m_animWalkLeft));
}

CEntityEnemy::~CEntityEnemy(void){}

void CEntityEnemy::doUpdate(void)
{
	if(m_healthPoint<=0)
	{
		m_active=false;
		CVector2i explosionPosition;
		explosionPosition.m_x = m_position.m_x+((m_rectangleCollision.m_right-m_rectangleCollision.m_left)/2)-23;
		explosionPosition.m_y = m_position.m_y+((m_rectangleCollision.m_bottom-m_rectangleCollision.m_top)/2)-23;
		CM.EXPLOSION().activeExplosion(eEnemyExplosion, explosionPosition);
	}

	m_previousPosition = m_position;

	if(m_goToTheRight)//if the enemy moves to the right
		m_position.m_x+=m_speed;
	else
		m_position.m_x-=m_speed;

	if(m_position.m_x>m_initialPosition.m_x+m_rangeOfDisplacement)
	{
		m_position.m_x = m_initialPosition.m_x+m_rangeOfDisplacement;
		m_goToTheRight = false;
		resetAnimData();
	}
	if(m_position.m_x<m_initialPosition.m_x-m_rangeOfDisplacement)
	{
		m_position.m_x = m_initialPosition.m_x-m_rangeOfDisplacement;
		m_goToTheRight = true;
		resetAnimData();
	}

	//check collision
	CWorldEntity* collidedWith = CM.WORLD().checkCollision(this);
	if(collidedWith!=0)
	{
		if(collidedWith->getSide()==eSidePlayer)
		{
			m_healthPoint-=collidedWith->getDamagePoint();
			collidedWith->setHealthPoint(collidedWith->getHealthPoint()-m_damagePoint);
		}
		m_position = m_previousPosition;
	}

	//apply gravity//
	//define the gravity vector
	const CVector2i gravity(0, 7);

	//save the previous position if dufferent of the current
	if(m_previousPosition != m_position)
		m_previousPosition = m_position;

	//apply gravity
	m_position=m_position+gravity;

	//check collision
	CWorldEntity* collisionEntity = CM.WORLD().checkCollision(this);
	if(collisionEntity!=0)
	{
		//get the height of the entity collided with
		int height=m_rectangleCollision.getBottom() - m_rectangleCollision.getTop();

		//get the top Y coordinate of the entity collided with
		int y=collisionEntity->getPosition().getY()-height;

		//set the position of the player
		m_position.setY(y-1);

	}
}

void CEntityEnemy::doRender(void)
{
	int idAnimToDisplay = 0;
	if(m_goToTheRight)
		idAnimToDisplay = m_animWalkRight;
	else
		idAnimToDisplay = m_animWalkLeft;

	CM.VISU().displayAnimation(idAnimToDisplay, m_position, &m_idAnimSprite, &m_animTime);
}

void CEntityEnemy::scrollEntity(const CVector2i& scrollVector)
{
	//CM.LOGCONSOLE().Log()<<"Scroll enemy\n";
	m_position = m_position + scrollVector;
	m_initialPosition = m_initialPosition + scrollVector;
}

void CEntityEnemy::resetAnimData(void)
{
	m_idAnimSprite = -1;
	m_animTime = -1;
}
